//
//  MainTabBarViewController.swift
//  MyWeibo
//
//  Created by 余丽丽 on 15/10/8.
//  Copyright © 2015年 余丽丽. All rights reserved.
//

import UIKit

class MainTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //添加所有的子控制器
        addChildViewControllers()
        

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //此时tabBar上面的按钮已经创建完成
        setupComposedButton()
        
    }
    ///  添加所有的子控制器
    private func addChildViewControllers(){
    //在这个函数中,只是添加控制器,没有创建tabbar的button,在willdidAppare中已经创建按钮
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }

    ///  添加子控制器
    ///
    ///  - parameter childController: 视图控制器
    ///  - parameter title:           标题
    ///  - parameter imageName:       图像名称
    private func addChildViewController(childController: UIViewController,title : String, imageName : String) {
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        //创建导航控制器为子控制器
        let nar = UINavigationController(rootViewController: childController)
        addChildViewController(nar)
        
    }
    
    //添加中间的+button
    private func setupComposedButton(){
        let weight = tabBar.bounds.width / CGFloat(viewControllers!.count)
        let rect = CGRect(x: weight * 2, y: 0, width: weight, height: tabBar.bounds.height)
        composeButton.frame  = rect
    
    }
    
    //懒加载
    lazy private var composeButton : UIButton = {
    let button = UIButton()
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //监听按钮的点击
        //print(UIControlEvents.TouchUpInside.rawValue)
//        button.addTarget(self, action: "clickComposedButton", forControlEvents:UIControlEvents(rawValue: 64))
        button.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)

        //懒加载中需要使用self,因为懒加载中需要指明对象的归属
        self.tabBar.addSubview(button)
        
        return button
    }()

    
    ///  点击中间按钮事件
    //private 表示这个方法只在此swift文件中可见
    //由于OC是运行时 而swift是静态语言,为了让这个私有方法拥有运行时的特征,需要在前面加上@objc修饰符,否则会报错说找不到此方法
      @objc private func clickComposedButton(){
        //根据用户是否登录来选择进入什么界面
        let vc = UserAccount.userLogon ? ComposeViewController() : OAuthViewController()
        let nav = UINavigationController(rootViewController: vc)
        presentViewController(nav, animated: true, completion: nil)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
